package com.smart.alipay.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.smart.alipay.exception.PayException;
import com.smart.alipay.service.AliPayService;
import com.smart.common.vo.ResponseEnumCode;
import com.smart.common.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class AliPayServiceImpl implements AliPayService {
    /**
     * 支付连接
     *
     * @param subject
     * @param orderSn
     * @param total
     * @return
     * @throws Exception
     */
    @Override
    public ResponseResult<String> pay(String subject, String orderSn, BigDecimal total) throws Exception {
        //发起支付  return  url  是前端支付成功之后跳转的界面  get
        AlipayTradeAppPayResponse response = Factory.Payment.App().pay(subject, orderSn, total.toString());
        // 3. 处理响应或异常
        if (ResponseChecker.success(response)) {
            log.info(response.body);
            return ResponseResult.success(response.body);
        } else {
            throw new PayException(ResponseEnumCode.PAY_FAIL_ERROR);
        }
    }
}
